package com.meituan;

import org.junit.Test;

import java.util.HashSet;
import java.util.Scanner;

/**
 * 美团第二题
 */
public class test2 {

    private static class Node {
        String from;
        String to;
        public Node(String from, String to){
            this.from = from;
            this.to = to;
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            Node[] nodes = new Node[n];
            for(int i = 0; i<n; i++){
                String from = sc.next();
                String to = sc.next();
                nodes[i] = new Node(from,to);
            }
            System.out.println(getRound(nodes));
        }

    }

    public static int getRound(Node[] nodes){
        int res = 0;
        HashSet<String> alreadyReach = new HashSet<>();
        for(Node node : nodes){
            String from = node.from;
            String to = node.to;
            if(alreadyReach.contains(to)){//发现环
                res++;
                alreadyReach.clear();
                continue;
            }
            alreadyReach.add(from);
            alreadyReach.add(to);
        }
        return res;
    }

    @Test
    public void test(){
        Node node1 = new Node("beijing","nanjing");
        Node node2 = new Node("nanjing","guangzhou");
        Node node3 = new Node("guangzhou","shanghai");
        Node node4 = new Node("shanghai","beijing");
        Node node5 = new Node("fuzhou","beijing");
        Node node6 = new Node("beijing","fuzhou");
        Node[] nodes = {node1,node2,node3,node4,node5,node6};
        System.out.println(getRound(nodes));
    }
}
